package cn.getech.data.development.service.impl;

import cn.getech.data.development.constant.JobLogTypeEnum;
import cn.getech.data.development.constant.LastRunState;
import cn.getech.data.development.dto.JobHistoryDto;
import cn.getech.data.development.entity.*;
import cn.getech.data.development.mapper.JobRunHistoryMapper;
import cn.getech.data.development.model.dto.JobLogDto;
import cn.getech.data.development.model.vo.JobDepRunHistoryVO;
import cn.getech.data.development.model.vo.JobRunHistoryVO;
import cn.getech.data.development.model.vo.WorkJobAndDepVO;
import cn.getech.data.development.service.*;
import cn.getech.data.development.utils.DataPermissionUtil;
import cn.getech.data.intelligence.common.utils.DateUtils;
import cn.getech.data.intelligence.common.utils.PageUtils;
import cn.getech.data.intelligence.common.utils.Query;
import cn.getech.system.center.utils.ShiroUtils;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jodd.util.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.text.DecimalFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName WorkFlowLogServiceImpl
 * @Description  运维中心-离线任务运维业务类
 * @Author Getech
 * @Date 2021/1/15 15:00
 */
@Service
public class WorkFlowLogServiceImpl implements WorkFlowLogService {


    @Autowired
    private JobNodeRunHistoryService jobNodeRunHistoryService;
    @Autowired
    private JobRunHistoryService jobRunHistoryService;
    @Autowired
    private JobNodeInfoService jobNodeInfoService;
    @Autowired
    private JobInfoService jobInfoService;
    @Autowired
    private JobDepRunHistoryService jobDepRunHistoryService;

    @Autowired
    private WorkFlowDepService workFlowDepService;

    @Autowired
    private IWorkMenuJobRelService iWorkMenuJobRelService;

    @Autowired
    private IWorkMenuService workMenuService;

    @Autowired
    private JobRunHistoryMapper jobRunHistoryMapper;



    /**
    * @Description 分页获取离线任务日志
    * @Author  chengweiping
    * @Date   2021/1/15 16:23
    */
    @Override
    public  PageUtils getWorkFlowLogByPage(JobLogDto downloadLogDto){
            //调度运行类型，设置为3，把手动和定时的运行任务都查出来
            Map<String,Object> params=new HashMap();
            params.put("proc_id",downloadLogDto.getProc_id());
            params.put("state",downloadLogDto.getState());
            params.put("jobLogType",downloadLogDto.getJob_log_type());
            params.put("queueName",downloadLogDto.getQueueName());
            params.put("name",downloadLogDto.getName());
            params.put("runType",3);
            params.put("page",downloadLogDto.getPage());
            params.put("limit",downloadLogDto.getLimit());
            params.put("jobStartTime",downloadLogDto.getJobStartTime());
            params.put("jobEndTime",downloadLogDto.getJobEndTime());
            params.put("tenantId", ShiroUtils.getTenantId());
            IPage<WorkJobAndDepVO> page = jobRunHistoryMapper.queryAllJobAndDepHistoryPage((Page<WorkJobAndDepVO>) new Query<WorkJobAndDepVO>().getPage(params), params);
            List<WorkJobAndDepVO>  workJobAndDepVOList=page.getRecords();

            /**
             *  按作业流与作业流依赖进行分组
             */
            Map<Integer,List<WorkJobAndDepVO>> jobGroup =workJobAndDepVOList.stream().parallel().collect(Collectors.groupingBy(WorkJobAndDepVO::getJobLogType));

            //取出作业流列表
            List<WorkJobAndDepVO>   workFlowList=  jobGroup.get(JobLogTypeEnum.WORKFLOW.getCode());

            //取出作业流依赖列表
            List<WorkJobAndDepVO>   workFlowDepList=  jobGroup.get(JobLogTypeEnum.WORKFLOWDEP.getCode());
            List<WorkJobAndDepVO>   resultList=new ArrayList<>();
           if(CollectionUtil.isNotEmpty(workFlowList)) {
               //填充作业流状态、耗时时间、状态名称
               fillJobRunHistoryInfo(workFlowList);
               resultList.addAll(workFlowList);
           }

            if(CollectionUtil.isNotEmpty(workFlowDepList)) {
                //填充作业流依赖状态、耗时时间、状态名称
                fillJobDepHistoryInfo(workFlowDepList);
                resultList.addAll(workFlowDepList);
            }

       //按开始时间倒序排序
      /* List<WorkJobAndDepVO> orderResultList= resultList.stream().sorted((a, b) -> {
            if(a.getBeginTime()==null || b.getBeginTime()==null){
                return 1;
            }
            boolean flag= a.getBeginTime().after(b.getBeginTime());
           int  orderNum= flag?1:0;
           return orderNum;
        }).collect(Collectors.toList());*/
        resultList.stream().forEach(e->{
            Integer runType= e.getRunType();
            if(runType!=null){
                if(runType==-1){
                    e.setCycleWay("定时调度");
                    e.setSheduleWay("调度");
                }else if(runType==-2){
                    e.setCycleWay("循环调度");
                    e.setSheduleWay("调度");
                }else if(runType==-3){
                    e.setCycleWay("cronTab调度");
                    e.setSheduleWay("调度");
                }else{
                    e.setSheduleWay("手动");
                }
            }
            e.setJobName(e.getName());
            e.setQueueName(e.getQueueName());
            e.setCronName(e.getCronName());
            if(StringUtil.isBlank(e.getCycleWay())){
                e.setCycleWay("--");
            }
            if(StringUtil.isBlank(e.getCronName())){
                e.setCronName("--");
            }
        });
            //设置结果
        page.setRecords(resultList);
        return new PageUtils(page);

    }


    /**
    * @Description 查询日志队列列表
    * @Author  chengweiping
    * @Date   2021/1/28 14:47
    */
    @Override
    public List<String>  getLogQueueList(){
        List<String> list = jobRunHistoryMapper.selectQueueNameList();
        if(CollectionUtil.isEmpty(list)){
            //如果数据差不多数据，则初始化一下数据
            list= CollUtil.newArrayList("root.users.hdfs","root.users.zhongtai","root.users.tongxun");
        }
       return list;
    }

    @Override
    public PageUtils getLogByPage(Map<String, Object> params) {
        //查询到所有的能够看到的数据---默认加载当前人创建的
        Long userId = ShiroUtils.getUserId();
        Object proc_id = params.get("proc_id");
        List<Integer> procIds = new ArrayList<>();
        if (ObjectUtils.isEmpty(proc_id)) {
            procIds = jobInfoService.selectProcIds();
        }else{
            procIds.add(Integer.valueOf(proc_id.toString()));
        }
        params.put("procIds",procIds);
        Object job_log_type = params.get("job_log_type");
        if(job_log_type == null){ //默认作业流
            params.put("job_log_type",0);
        }
//        Object userId1 = params.get("userId");
//        if(userId1 == null){
//            //默认当前用户。如果是租户管理员，则不用初始化用户
//            if(!DataPermissionUtil.isAdmin(userId.intValue())){
//                params.put("userId",userId);
//            }
//        }
        Object startTime = params.get("startTime"); //开始时间
        if(startTime == null){
            Date tenBefDate = tenBefDate(-10);
            params.put("startTime",tenBefDate); //默认10天前的数据
        }

        IPage<JobHistoryDto> page = jobRunHistoryMapper.queryAllJobsHistoryPage((Page<JobHistoryDto>) new Query<JobHistoryDto>().getPage(params), params);
        //挑选出来对应的类型，进行分组
        if(null != page &&  CollectionUtil.isNotEmpty(page.getRecords())){
            List<JobHistoryDto> records = page.getRecords();
            List<JobHistoryDto> list = new ArrayList<>();
            List<JobHistoryDto> depList = new ArrayList<>();
            for(int i = 0;i<records.size();i++){
                JobHistoryDto jobHistoryDto = records.get(i);
                jobHistoryDto.setOllled(i);//排序
                if(Objects.equals(jobHistoryDto.getJobLogType(),JobLogTypeEnum.WORKFLOW.getCode())){
                    list.add(jobHistoryDto);
                }else{
                    depList.add(jobHistoryDto);
                }
            }
            //进行分组赋值
            List<JobHistoryDto> allList = new ArrayList<>();
            if(CollectionUtil.isNotEmpty(list)){
                fillJobRunHistoryInfoNew(list);
                allList.addAll(list);
            }
            if(CollectionUtil.isNotEmpty(depList)){
                fillJobDepHistoryInfoNew(depList);
                allList.addAll(depList);
            }
            page.setRecords(allList.stream().sorted(Comparator.comparing(JobHistoryDto::getOllled)).collect(Collectors.toList()));
        }
        return new PageUtils(page);
    }

    private Date tenBefDate(int days) {
        Date date = DateUtils.addDateDays(new Date(), days);
        String befDates = DateUtils.format(date) + " 00:00:00";
        return DateUtils.stringToDate(befDates,DateUtils.DATE_TIME_PATTERN);
    }

    private void initDataList() {
        //250135
        JobRunHistory jobRunHistory = jobRunHistoryService.getById(250023);
        List<JobRunHistory> lists = new ArrayList<>();
        for (int i= 0;i<100001;i++){
            JobRunHistory tm = new JobRunHistory();
            BeanUtils.copyProperties(jobRunHistory,tm);
            tm.setId(null);
            tm.setOozieJobId(jobRunHistory.getOozieJobId()+i);
            lists.add(tm);
            if(i%1000 == 0){
                System.out.println(i);
                jobRunHistoryService.saveBatch(lists);
                lists.clear();
            }
        }
    }

    private void fillJobRunHistoryInfoNew(List<JobHistoryDto> list) {
        DecimalFormat df = new DecimalFormat("0.00");
        Map<Integer, JobRunHistoryVO> map = getEachJobNodeStateIds(list);
        List<Integer> jobIds = list.stream().map(JobHistoryDto::getJobInfoId).collect(Collectors.toList());
        //查询这个工作流对应的菜单id
        List<WorkMenuJobRel> workMenuJobRel = iWorkMenuJobRelService.list(new QueryWrapper<WorkMenuJobRel>().eq("work_type",
                1).in("work_flow_id", jobIds));
        Map<Integer, Long> workMap = new HashMap<>();
        if (CollectionUtil.isNotEmpty(workMenuJobRel)) {
            workMap = workMenuJobRel.stream().collect(
                    Collectors.toMap(s -> s.getWorkFlowId().intValue(), WorkMenuJobRel::getWorkMenuId));
        }
        for (JobHistoryDto item : list) {
            Integer reRunType = item.getReRunType();
            if(null == reRunType){
                reRunType = 0;
            }
            if(reRunType > 0){
                reRunType = 1;
            }
            item.setReRunType(reRunType);
            item.setStateName(LastRunState.ObjOf(item.getState()).getName());
            item.setJobLogType(JobLogTypeEnum.WORKFLOW.getCode());
            JobRunHistoryVO jobRunHistoryVO = map.get(item.getId());
            item.setNodeStates(jobRunHistoryVO.getNodeStates());
            item.setNodeOutHours(jobRunHistoryVO.getNodeOutHours());
            if (null != item.getCost()) {
                item.setCostSecond(df.format((float) item.getCost() / 1000));
            }
            item.setWorkMenuId(workMap.get(item.getJobInfoId()));
        }
    }


    private void fillJobRunHistoryInfo(List<WorkJobAndDepVO> list) {
        DecimalFormat df = new DecimalFormat("0.00");
     //   Map<Integer, JobRunHistoryVO> map = getEachJobNodeStateIds(list);
        List<Integer> jobIds = list.stream().parallel().map(WorkJobAndDepVO::getJobInfoId).collect(Collectors.toList());
        //查询这个工作流对应的菜单id
        List<WorkMenuJobRel> workMenuJobRel = iWorkMenuJobRelService.list(new QueryWrapper<WorkMenuJobRel>().eq("work_type",
                1).in("work_flow_id", jobIds));
        Map<Integer, Long> workMap = new HashMap<>();
        if (CollectionUtil.isNotEmpty(workMenuJobRel)) {
            workMap = workMenuJobRel.stream().collect(
                    Collectors.toMap(s -> s.getWorkFlowId().intValue(), WorkMenuJobRel::getWorkMenuId));
        }
        for (WorkJobAndDepVO item : list) {
            item.setStateName(LastRunState.ObjOf(item.getState()).getName());
            item.setJobLogType(JobLogTypeEnum.WORKFLOW.getCode());
         /*   JobRunHistoryVO jobRunHistoryVO = map.get(item.getId());
            item.setNodeStates(jobRunHistoryVO.getNodeStates());*/
            if (null != item.getCost()) {
                item.setCostSecond(df.format((float) item.getCost() / 1000));
            }
            item.setWorkMenuId(workMap.get(item.getJobInfoId()));
        }
    }

    private void fillJobDepHistoryInfoNew(List<JobHistoryDto> list) {
        DecimalFormat df = new DecimalFormat("0.00");
        List<Long> depJobIds = list.stream().map(JobHistoryDto::getJobDepId).collect(Collectors.toList());
        List<WorkFlowDep> depList = workFlowDepService.list(new QueryWrapper<WorkFlowDep>().in("id", depJobIds));
        List<Long> menuIds = depList.stream().map(WorkFlowDep::getWorkMenuId).collect(Collectors.toList());
        List<WorkMenu> menuList = workMenuService.list(new QueryWrapper<WorkMenu>().in("id", menuIds));
        HashMap<Long, WorkFlowDep> depListMap = new HashMap<>();
        for (WorkFlowDep workFlowDep : depList) {
            depListMap.put(workFlowDep.getId(),workFlowDep);
        }
        HashMap<Long, WorkMenu> menuListMap = new HashMap<>();
        for (WorkMenu workFlowDep : menuList) {
            menuListMap.put(workFlowDep.getId(),workFlowDep);
        }
        for (JobHistoryDto item :list){
            WorkFlowDep workFlowDep = depListMap.get(item.getJobDepId());
            if(workFlowDep==null){
                continue;
            }
            WorkMenu workMenu = menuListMap.get(workFlowDep.getWorkMenuId());
            if(null == workMenu){
                item.setDepName("已删除的作业流依赖");
            }else{
                item.setDepName(workMenu.getName());
            }
            Integer reRunType = item.getReRunType();
            if(null == reRunType){
                reRunType = 0;
            }
            if(reRunType > 0){
                reRunType = 1;
            }
            item.setReRunType(reRunType);
            item.setJobLogType(JobLogTypeEnum.WORKFLOWDEP.getCode());
            item.setStateName(LastRunState.ObjOf(item.getState()).getName());
            JobDepRunHistoryVO depRunHistoryVO = new JobDepRunHistoryVO();
            BeanUtils.copyProperties(item,depRunHistoryVO);
            depRunHistoryVO = getEachDepNodeState(item.getJobDepId().intValue(),item.getId(), depRunHistoryVO, item.getOozieJobId());
            item.setNodeStates(depRunHistoryVO.getNodeStates());
            item.setNodeOutHours(depRunHistoryVO.getNodeOutHours());
            if(null != item.getCost()){
                item.setCostSecond(df.format((float)item.getCost()/1000));
            }
            item.setWorkMenuId(workFlowDep.getWorkMenuId());
        }
    }

    private void fillJobDepHistoryInfo(List<WorkJobAndDepVO> list) {
        DecimalFormat df = new DecimalFormat("0.00");
        List<Long> depJobIds = list.stream().map(WorkJobAndDepVO::getJobDepId).collect(Collectors.toList());
        List<WorkFlowDep> depList = workFlowDepService.list(new QueryWrapper<WorkFlowDep>().in("id", depJobIds));
        List<Long> menuIds = depList.stream().map(WorkFlowDep::getWorkMenuId).collect(Collectors.toList());
        List<WorkMenu> menuList = workMenuService.list(new QueryWrapper<WorkMenu>().in("id", menuIds));
        HashMap<Long, WorkFlowDep> depListMap = new HashMap<>();
        for (WorkFlowDep workFlowDep : depList) {
            depListMap.put(workFlowDep.getId(),workFlowDep);
        }
        HashMap<Long, WorkMenu> menuListMap = new HashMap<>();
        for (WorkMenu workFlowDep : menuList) {
            menuListMap.put(workFlowDep.getId(),workFlowDep);
        }
        for (WorkJobAndDepVO item :list){
            WorkFlowDep workFlowDep = depListMap.get(item.getJobDepId());
            if(workFlowDep==null){
                continue;
            }
            WorkMenu workMenu = menuListMap.get(workFlowDep.getWorkMenuId());
            if(null == workMenu){
                item.setDepName("已删除的作业流依赖");
            }else{
                item.setDepName(workMenu.getName());
            }
            item.setJobLogType(JobLogTypeEnum.WORKFLOWDEP.getCode());
            item.setStateName(LastRunState.ObjOf(item.getState()).getName());
            if(null != item.getCost()){
                item.setCostSecond(df.format((float)item.getCost()/1000));
            }
            item.setWorkMenuId(workFlowDep.getWorkMenuId());
        }
    }



    public Map<Integer,JobRunHistoryVO> getEachJobNodeStateIds(List<JobHistoryDto> list){
        Map<Integer, JobRunHistoryVO> jobRunHistoryVOMap = new HashMap<>();
        List<Integer> jobId = list.stream().map(JobHistoryDto::getJobInfoId).collect(Collectors.toList());
        List<Integer> jobRunIds = list.stream().map(JobHistoryDto::getId).collect(Collectors.toList());
        List<String> ozId = list.stream().map(JobHistoryDto::getOozieJobId).collect(Collectors.toList());
        List<JobNodeInfo> jobNodeInfos = jobNodeInfoService.list(new QueryWrapper<JobNodeInfo>().in("job_id", jobId));
        Map<Integer, List<JobNodeInfo>> jobNodeInfosMap = new HashMap<>();
        if (CollectionUtil.isNotEmpty(jobNodeInfos)) {
            jobNodeInfosMap = jobNodeInfos.stream().collect(Collectors.groupingBy(JobNodeInfo::getJobId));
        }
        Map<Integer, List<JobRunHistory>> jobRunHistoryMap = new HashMap<>();
        List<JobRunHistory> jobRunHistory = jobRunHistoryService.list(new QueryWrapper<JobRunHistory>().in("id", jobRunIds));
        if (CollectionUtil.isNotEmpty(jobRunHistory)) {
            jobRunHistoryMap = jobRunHistory.stream().collect(Collectors.groupingBy(JobRunHistory::getId));
        }
        Map<String, List<JobNodeRunHistory>> jobNodeRunHistoryListMap = new HashMap<>();
        List<JobNodeRunHistory> jobNodeRunHistoryList = jobNodeRunHistoryService.list(new QueryWrapper<JobNodeRunHistory>()
                .in("oozie_job_id", ozId));
        if (CollectionUtil.isNotEmpty(jobNodeRunHistoryList)) {
            jobNodeRunHistoryListMap = jobNodeRunHistoryList.stream().collect(Collectors.groupingBy(JobNodeRunHistory::getOozieJobId));
        }
        for (JobHistoryDto runHistory : list) {
            JobRunHistoryVO jobRunHistoryVO = new JobRunHistoryVO();
            BeanUtils.copyProperties(runHistory,jobRunHistoryVO);
            if (CollectionUtil.isEmpty(jobNodeRunHistoryListMap.get(runHistory.getOozieJobId()))
                    && CollectionUtil.isEmpty(jobRunHistoryMap.get(runHistory.getId()))) {
                jobRunHistoryVO.setState(LastRunState.NOTRUN.getCode());
                jobRunHistoryVO.setLog("当前版本未执行");
            } else {
                Map<String, Integer> nodeStates = new HashMap<>();
                List<JobNodeRunHistory> ozJobList = jobNodeRunHistoryListMap.get(runHistory.getOozieJobId());
                if (CollectionUtil.isNotEmpty(ozJobList)) {
                    for (JobNodeRunHistory jobNodeRunHistory : ozJobList) {
                        if (null == jobNodeRunHistory &&
                                CollectionUtil.isEmpty(jobRunHistoryMap.get(runHistory.getReRunNodeId()))
                        ) {
                            nodeStates.put(jobNodeRunHistory.getJobNodeInfoId().toString(), LastRunState.NOTRUN.getCode());
                        } else {
                            nodeStates.put(jobNodeRunHistory.getJobNodeInfoId().toString(), jobNodeRunHistory.getState());
                        }
                    }
                }
                List<JobNodeInfo> nodeNodeInfo = jobNodeInfosMap.get(runHistory.getJobInfoId());
                if (CollectionUtil.isNotEmpty(nodeNodeInfo)) {
                    Set<String> nodeKeys = nodeNodeInfo.stream().map(s -> s.getId().toString()).collect(Collectors.toSet());
                    //将未查询到的node状态赋值为未运行
                    nodeKeys.removeAll(nodeStates.keySet());
                    for (String s : nodeKeys
                    ) {
                        nodeStates.put(s, LastRunState.NOTRUN.getCode());
                    }
                    jobRunHistoryVO.setNodeStates(nodeStates);
                }
            }
            jobRunHistoryVOMap.put(runHistory.getId(), jobRunHistoryVO);
        }
        return jobRunHistoryVOMap;
    }


    public JobRunHistoryVO getEachJobNodeState(Integer jobId, Integer jobRunId, JobRunHistoryVO jobRunHistoryVO,String oozieId){
        List<JobNodeInfo> jobNodeInfos = jobNodeInfoService.list(new QueryWrapper<JobNodeInfo>().eq("job_id",jobId));
        Set<String> nodeKeys = new HashSet<>();
        for (JobNodeInfo jobNodeInfo :  jobNodeInfos
        ) {
            nodeKeys.add(jobNodeInfo.getId().toString());
        }
        JobRunHistory jobRunHistory = jobRunHistoryService.getOne(new QueryWrapper<JobRunHistory>().eq("id", jobRunId));
        List<JobNodeRunHistory> jobNodeRunHistorys = jobNodeRunHistoryService.list(new QueryWrapper<JobNodeRunHistory>()
                .eq("oozie_job_id",oozieId));
        if(CollectionUtil.isEmpty(jobNodeRunHistorys) && jobRunHistory == null){
            jobRunHistoryVO.setState(LastRunState.NOTRUN.getCode());
            jobRunHistoryVO.setLog("当前版本未执行");
            return jobRunHistoryVO;
        }
        Map<String,Integer> nodeStates = new HashMap<>();
        for (JobNodeRunHistory jobNodeRunHistory : jobNodeRunHistorys) {
            if(null == jobNodeRunHistory && jobRunHistory == null){
                nodeStates.put(jobNodeRunHistory.getJobNodeInfoId().toString(),LastRunState.NOTRUN.getCode());
            }else {
                nodeStates.put(jobNodeRunHistory.getJobNodeInfoId().toString(),jobNodeRunHistory.getState());
            }
        }
        //将未查询到的node状态赋值为未运行
        nodeKeys.removeAll(nodeStates.keySet());
        for (String s : nodeKeys
        ) {
            nodeStates.put(s,LastRunState.NOTRUN.getCode());
        }
        jobRunHistoryVO.setNodeStates(nodeStates);
        return jobRunHistoryVO;
    }


    public JobDepRunHistoryVO getEachDepNodeState(Integer jobDepId, Integer jobDepRunId, JobDepRunHistoryVO jobDepRunHistoryVO,String depJobOozieId){
        WorkFlowDep workFlowDep =workFlowDepService.getById(jobDepId);

        List<WorkMenuJobRel> workMenuJobRels = iWorkMenuJobRelService.list(new QueryWrapper<WorkMenuJobRel>().eq("work_type", 2).eq("work_menu_id", workFlowDep.getWorkMenuId()));
        Set<String> jobIds = new HashSet<>();
        for (WorkMenuJobRel workMenuJobRel : workMenuJobRels) {
            jobIds.add(workMenuJobRel.getWorkFlowId().toString());
        }
        List<Integer> jobIdList = new ArrayList<>();
        jobIds.forEach(j->{
            jobIdList.add(Integer.parseInt(j));
        });

//        List<JobInfo> jobInfos = jobInfoService.list(new QueryWrapper<JobInfo>().in("id",jobIdList));

        JobDepHistory jobDepHistory = jobDepRunHistoryService.getOne(new QueryWrapper<JobDepHistory>().eq("id", jobDepRunId));
        List<JobRunHistory> jobRunHistories = jobRunHistoryService.list(new QueryWrapper<JobRunHistory>()
                .eq("dep_oozie_job_id",depJobOozieId));
        if(CollectionUtil.isEmpty(jobRunHistories) && jobDepHistory == null){
            jobDepRunHistoryVO.setState(LastRunState.NOTRUN.getCode());
            jobDepRunHistoryVO.setLog("当前版本未执行");
            return jobDepRunHistoryVO;
        }
        Map<String,Integer> nodeStates = new HashMap<>();
        for (JobRunHistory jobRunHistory : jobRunHistories) {
            if(null == jobRunHistory && jobDepHistory == null){
                nodeStates.put(jobRunHistory.getJobInfoId().toString(),LastRunState.NOTRUN.getCode());
            }else {
                nodeStates.put(jobRunHistory.getJobInfoId().toString(),jobRunHistory.getState());
            }
        }
        //将未查询到的node状态赋值为未运行
        jobIds.removeAll(nodeStates.keySet());
        for (String s : jobIds
        ) {
            nodeStates.put(s,LastRunState.NOTRUN.getCode());
        }
        jobDepRunHistoryVO.setNodeStates(nodeStates);
        return jobDepRunHistoryVO;
    }

}
